﻿Imports System.Drawing.Drawing2D

Partial Class TabStrip

	Public Class Tab
		Public Rect As Rectangle
		Public Locked As Boolean
		Public Strip As TabStrip
		Public Window As Window
		Public LastActive As Control

		Sub Draw(ByVal G As Graphics, ByVal IsSelected As Boolean, ByVal IsHovered As Boolean)

			Dim spread As Integer
			Dim eigth As Integer
			Dim sixth As Integer
			Dim quarter As Integer
			Dim R = Me.Rect
			G.SmoothingMode = SmoothingMode.HighQuality
			G.PixelOffsetMode = PixelOffsetMode.HighQuality
			spread = CInt(Math.Truncate(Math.Floor(CDec(R.Height) * 2 / 3)))
			eigth = CInt(Math.Truncate(Math.Floor(CDec(R.Height) * 1 / 8)))
			sixth = CInt(Math.Truncate(Math.Floor(CDec(R.Height) * 1 / 6)))
			quarter = CInt(Math.Truncate(Math.Floor(CDec(R.Height) * 1 / 4)))
			Dim Path As New GraphicsPath
			Path.AddCurve(New Point() {New Point(R.X, R.Bottom), New Point(R.X + sixth, R.Bottom - eigth), New Point(R.X + spread - quarter, R.Y + sixth), New Point(R.X + spread, R.Y)})
			Path.AddLine(R.X + spread, R.Y, R.Right - spread, R.Y)
			Path.AddCurve(New Point() {New Point(R.Right - spread, R.Y), New Point(R.Right - spread + quarter, R.Y + sixth), New Point(R.Right - sixth, R.Bottom - eigth), New Point(R.Right, R.Bottom)})
			Dim col = Themes.Current.FormColor
			col = col.Mix(Color.White, Opacity * 100)
			If IsSelected Then
				Dim B = LGBv(Me.Rect, Color.White, Themes.Current.FormColor.Mix(Drawing.Color.White, 50))

				G.FillPath(B, Path)
			Else
				G.FillPath(col.Brush, Path)
			End If

			G.DrawPath(col.Mix(Color.Black, 50).Pen, Path)

			Dim F As StringFormat = StrFormat("l", "c")
			F.Trimming = StringTrimming.EllipsisWord
			F.FormatFlags = F.FormatFlags Or StringFormatFlags.NoWrap
			G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
			G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighSpeed
			Dim Rect = Me.Rect
			Rect.Inflate(-8, 0)
			Dim ImageRect As New Rectangle(Rect.Left, Rect.Top, Rect.Height, Rect.Height)
			Dim Image As Image = Nothing
			If Me Is Strip.NewTab Then
				Image = My.Resources.add
				ImageRect.Offset(-ImageRect.Left + Me.Rect.Left + (Width - ImageRect.Width) / 2, 0)
			ElseIf Not Window Is Nothing Then
				Image = Window.Image
			End If
			'If Image Is Nothing Then ImageRect.Width = 0
			ImageRect.Inflate(-5, -5)
			'If Rect.Width > 2 * Rect.Height Then
			If Not Image Is Nothing Then DrawImage(G, Image, ImageRect, If(Strip.SelectedTab Is Me Or Strip.HoverTab Is Me, 1, (1 + Opacity) / 2))
			'End If
			Dim TextRec As New Rectangle(ImageRect.Right + 3, Rect.Top, Rect.Right - ImageRect.Right - 8, Rect.Height)
			If IsSelected And Not Locked Then TextRec.Width -= Rect.Height - 4
			If IsSelected Then
				DrawText2(G, Text, Strip.Font, Color.Black, TextRec, F)
			ElseIf IsHovered Then
				DrawText2(G, Text, Strip.Font, Color.Black, TextRec, F)
			Else
				DrawText2(G, Text, Strip.Font, Color.Black, TextRec, F)
			End If
			If Rect.Width > 2 * Rect.Height Then
				If IsSelected And Not Locked Then
					R = CloseRect
					If Strip.HoverClose Then Renderer.PaintButton(G, R, Nothing, False, IsSelected, False, True)
					G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
					G.SmoothingMode = SmoothingMode.HighQuality
					R.Inflate(-3, -3)
					If R.Width > 0 Then
						Dim P2 As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
						R.Inflate(-2, -2)
						P2.AddLine(R.Left, R.Top, R.Right, R.Bottom)
						P2.StartFigure()
						P2.AddLine(R.Left, R.Bottom, R.Right, R.Top)
						G.DrawPath(RoundedPen(Color.FromArgb(128, Color.Black), 4), P2)
						G.DrawPath(RoundedPen(Color.FromArgb(255 * Opacity, Color.White), 2), P2)
					End If
				End If
			End If
			G.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
			G.SmoothingMode = SmoothingMode.None
		End Sub
		'	Dim curve = 7
		'	If Rect.Width = 0 Then Exit Sub
		'	Dim F As StringFormat = StrFormat("l", "c")
		'	F.Trimming = StringTrimming.EllipsisWord
		'	F.FormatFlags = F.FormatFlags Or StringFormatFlags.NoWrap
		'	G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		'	G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighSpeed
		'	If IsSelected Then
		'		FillRoundRect(G, Rect, New SolidBrush(Color.FromArgb(255, 192, 0)), curve)
		'	Else
		'		FillRoundRect(G, Rect, New SolidBrush(Color.FromArgb(Opacity * 192, Color.White)), curve)
		'	End If
		'	Dim ImageRect As New Rectangle(Rect.Left, Rect.Top, Rect.Height, Rect.Height)
		'	Dim Image As Image = Nothing
		'	If Me Is Strip.NewTab Then
		'		Image = My.Resources.add
		'		ImageRect.Offset((Width - ImageRect.Width) / 2, 0)
		'	ElseIf Not Window Is Nothing Then
		'		Image = Window.Image
		'	End If
		'	'If Image Is Nothing Then ImageRect.Width = 0
		'	ImageRect.Inflate(-5, -5)
		'	'If Rect.Width > 2 * Rect.Height Then
		'	If Not Image Is Nothing Then DrawImage(G, Image, ImageRect, If(Strip.SelectedTab Is Me Or Strip.HoverTab Is Me, 1, Opacity))
		'	'End If
		'	Dim TextRec As New Rectangle(ImageRect.Right + 3, Rect.Top, Rect.Right - ImageRect.Right - 8, Rect.Height)
		'	If IsSelected And Not Locked Then TextRec.Width -= Rect.Height - 4
		'	If IsSelected Then
		'		DrawText2(G, Text, Strip.Font, Color.Black, TextRec, F)
		'	ElseIf IsHovered Then
		'		DrawText2(G, Text, Strip.Font, Color.Black, TextRec, F)
		'	Else
		'		DrawText2(G, Text, Strip.Font, Color.Black, TextRec, F)
		'	End If
		'	If Rect.Width > 2 * Rect.Height Then
		'		If IsSelected And Not Locked Then
		'			Dim R As Rectangle = CloseRect
		'			If R.Width > 0 Then
		'				Dim P2 As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
		'				R.Inflate(-2, -2)
		'				P2.AddLine(R.Left, R.Top, R.Right, R.Bottom)
		'				P2.StartFigure()
		'				P2.AddLine(R.Left, R.Bottom, R.Right, R.Top)
		'				G.DrawPath(RoundedPen(Color.FromArgb(128, Color.Black), 4), P2)
		'				G.DrawPath(RoundedPen(Color.FromArgb(255 * Opacity, Color.White), 2), P2)
		'			End If
		'		End If
		'	End If
		'	G.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
		'	Dim R1 = Rect
		'	DrawRoundRect(G, R1, New Pen(Color.FromArgb(If(IsSelected, 255, 128) * Opacity, Color.White)), curve)
		'	R1.Inflate(-1, -1)
		'	DrawRoundRect(G, R1, New Pen(Color.FromArgb(16, Color.Black)), curve - 1)
		'End Sub

		Property Width As Integer
			Get
				Return Rect.Width
			End Get
			Set(Value As Integer)
				'If Value < 80 Then Value = 80
				Rect.Width = Value
				Strip.CalculateTabPositions()
			End Set
		End Property

		ReadOnly Property CloseRect() As Rectangle
			Get
				If Locked Then Return New Rectangle(0, 0, 0, 0)
				Dim R As New Rectangle(Rect.Right - Rect.Height - 8, Rect.Top, Rect.Height, Rect.Height)
				R.Inflate(-3, -3)
				'R.Offset(-2, -3)
				Return R
			End Get
		End Property

		Sub Activate()
			Strip.SelectedTab = Me
		End Sub

		Dim _Text As String
		Property Text As String
			Get
				Return _Text
			End Get
			Set(value As String)
				_Text = value
				Invalidate()
				'Animator.Run(Me, "Width", 0, ActualWidth, 0.2, ActualWidth)
			End Set
		End Property

		Sub Close()
			Strip.CloseTab(Me)
		End Sub

		Dim ActualWidth As Integer
		Sub Measure()
			ActualWidth = Math.Min(Strip.Width / Strip.Tabs.Count, 200)
			'ActualWidth = G.MeasureString(Text, Strip.Font).Width + Rect.Height * 2
		End Sub

		Dim _Opacity As Double = 0
		Property Opacity As Double
			Get
				Return _Opacity
			End Get
			Set(value As Double)
				_Opacity = value
				Invalidate()
			End Set
		End Property

		Sub Invalidate()
			Dim R = Rect
			R.Inflate(16, 1)
			Strip.Invalidate(R)
		End Sub

	End Class

End Class
